<template>
  <div>
    <el-container>
      <el-main>
        <div style="margin-bottom:15px;text-align: left">
          <el-input style="width: 240px" placeholder="站点名称" size="small" v-model="params.sitename"></el-input>
          <el-input style="width: 240px; margin-left: 15px" placeholder="站点电话" size="small"
                    v-model="params.sitephone"></el-input>
          <el-button style="margin-left: 15px" size="small" type="primary" @click="getall"><i
              class="el-icon-search"></i> 搜索
          </el-button>
          <el-button style="margin-left: 15px" size="small" type="warning" @click="reset"><i
              class="el-icon-refresh"></i> 重置
          </el-button>
        </div>
        <div style="text-align: left">
          <el-button style="margin-left: 0px" size="small" type="primary" @click="addinfo"><i
              class="el-icon-circle-plus-outline"></i> 添加信息
          </el-button>
        </div>
        <el-table
            :border="true"
            :data="tableData"
            stripe
            style="width: 100%;margin-top: 20px">
          <el-table-column
              prop="id"
              label="编号"
              width="60">
          </el-table-column>
          <el-table-column
              prop="sitename"
              label="站点名称"
              width="180">
          </el-table-column>
          <el-table-column
              prop="store"
              label="总库存"
              width="100">
          </el-table-column>
          <el-table-column
              prop="type"
              label="类型(支持筛选)"
              width="150"
              :filters="[{ text: '全血', value: '全血' }, { text: '成分血', value: '成分血' }]"
              :filter-method="filterTag"
              filter-placement="bottom-end">
            <template slot-scope="scope">
              <el-tag
                  :type="scope.row.type === '成分血' ? 'primary' : 'success'"
                  disable-transitions>{{ scope.row.type === '全血' ? "全血" : "成分血" }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
              prop="siteaddress"
              label="站点地址"
              width="300">
          </el-table-column>
          <el-table-column
              prop="sitephone"
              label="站点电话"
              width="100">
          </el-table-column>
          <el-table-column
              prop="sitepage"
              label="友情链接(点击直达)"
              width="240">
            <template slot-scope="scope">
              <a @click.prevent="handleLinkClick(scope.row)" target="_blank">
                {{ scope.row.sitepage }}
              </a>
            </template>
          </el-table-column>
          <el-table-column
              prop="status"
              label="状态"
              width="80">
          </el-table-column>
          <el-table-column label="操作" width="200">
            <template #default="scope">
              <!--                        scope.row 就是当前行数据-->
              <el-button type="primary" size="small" @click="handleEdit(scope.row)"><i class="el-icon-edit-outline"></i>
                编 辑
              </el-button>
              <el-popconfirm
                  style="margin-left: 5px"
                  title="您确定删除这行数据吗？"
                  @confirm="del(scope.row.id)"
              >
                <el-button type="danger" size="small" slot="reference"><i class="el-icon-delete"></i> 删 除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!--    分页-->
        <div style="margin-top: 20px">
          <el-pagination
              background
              :current-page="params.pageNum"
              :page-size="params.pageSize"
              layout="prev, pager, next, jumper"
              @current-change="handleCurrentChange"
              :total="total">
          </el-pagination>
        </div>
        <!--      修改-->
        <div style="margin: 0px 0">
          <el-dialog
              :visible.sync="centerDialogVisibleEdit"
              title="修改"
              width="30%"
              center>
            <el-form :model="form" ref="ruleForm" :rules="rules" label-width="80px">
              <el-form-item label="站点名称">
                <el-input v-model="form.sitename" style="width: 90%"/>
              </el-form-item>
              <el-form-item label="友情链接">
                <el-input v-model="form.sitepage" style="width: 90%"/>
              </el-form-item>
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="类型">
                    <el-select v-model="form.type" style="width: 100%" placeholder="请选择">
                      <el-option
                          v-for="item in types"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="状态">
                    <el-select v-model="form.status" style="width: 72%" placeholder="请选择">
                      <el-option
                          v-for="item in statuses"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        <span style="float: left">{{ item.label }}</span>
                        <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="站点电话">
                    <el-input v-model="form.sitephone" style="width: 100%" disabled/>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="总库存">
                    <el-input v-model="form.store" style="width: 72%" disabled/>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item label="站点地址">
                <el-input v-model="form.siteaddress" style="width: 90%;" disabled/>
              </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                  <el-button @click="centerDialogVisibleEdit = false">取消</el-button>
                  <el-button type="primary" @click="save">确认</el-button>
                </span>
            </template>
          </el-dialog>
        </div>
        <!--      添加-->
        <div style="margin: 0px 0">
          <el-dialog
              :visible.sync="centerDialogVisibleAdd"
              title="添加"
              width="30%"
              center>
            <el-form :model="addform" ref="ruleForm" :rules="rules" label-width="80px">
              <el-form-item label="站点名称">
                <el-input v-model="addform.sitename" style="width: 90%"/>
              </el-form-item>
              <el-form-item label="友情链接">
                <el-input v-model="addform.sitepage" style="width: 90%"/>
              </el-form-item>
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="类型">
                    <el-select v-model="addform.type" style="width: 100%" placeholder="请选择">
                      <el-option
                          v-for="item in types"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        <span style="float: left">{{ item.label }}</span>
                        <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="状态">
                    <el-select v-model="addform.status" style="width: 70%" placeholder="请选择">
                      <el-option
                          v-for="item in statuses"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        <span style="float: left">{{ item.label }}</span>
                        <!--                        <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>-->
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="站点电话">
                    <el-input v-model="addform.sitephone" style="width: 100%"/>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="总库存">
                    <el-input v-model="addform.store" style="width: 70%"/>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item label="站点地址">
                <el-input v-model="addform.siteaddress" style="width: 90%;"/>
              </el-form-item>
            </el-form>
            <template #footer>
              <span class="dialog-footer">
                <el-button @click="centerDialogVisibleAdd = false">取消</el-button>
                <el-button type="primary" @click="confirmadd">确认</el-button>
              </span>
            </template>
          </el-dialog>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  data() {
    return {
      tableData: [],
      total: 0,
      params: {
        pageNum: 1,
        pageSize: 7,
        sitename: '',
        sitephone: ''
      },
      types: [{
        value: '全血',
        label: '全血 Whole'
      }, {
        value: '成分血',
        label: '成分血 Component'
      }],
      statuses: [{
        value: '开放中',
        label: '开放中'
      }, {
        value: '未开放',
        label: '未开放'
      }],
      form: {
        sitename: '',
        store: '',
        type: '',
        siteaddress: '',
        sitephone: '',
        sitepage: '',
        status: ''
      },
      addform: {
        sitename: '',
        store: '',
        type: '',
        siteaddress: '',
        sitephone: '',
        sitepage: '',
        status: ''
      },
      rules: {
        sitename: [{required: true, message: '请输入名称', trigger: 'blur'}],
        siteaddress: [{required: true, message: '请输入地址', trigger: 'blur'}],
      },
      centerDialogVisibleEdit: false, //编辑弹窗
      centerDialogVisibleAdd: false, //添加弹窗
    }
  },
  created() {
    this.getall()
  },
  methods: {
    getall() {
      request.get('sites/page', {
        params: this.params
      }).then(res => {
        if (res.code === '200') {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      })
    },
    reset() {
      this.params = {
        pageNum: 1,
        pageSize: 7,
        donors: '',
        bloodType: ''
      }
      this.getall()
    },
    del(id) {
      request.delete('sites/deleteOne/' + id).then(res => {
        if (res.code === '200') {
          this.$notify.success('删除成功')
          this.getall()
        } else {
          this.$notify.error(res.msg)
        }
      })
    },
    confirmadd() {
      request.post('sites/addOne', this.addform).then(res => {
        if (res.code === '200') {
          this.$message({
            type: "success",
            message: "添加信息成功！"
          })
          this.addform = {}
          this.centerDialogVisibleAdd = false
          this.getall()
        } else {
          this.$message({
            type: "warning",
            message: res.msg
          })
          this.addform = {}
          this.centerDialogVisibleAdd = false
          this.getall()
        }
      })
    },
    async save() {
      this.form.optioner = JSON.parse(localStorage.getItem('name')).name
      if (this.form.id) {
        this.$refs['ruleForm'].validate((valid) => {
          if (valid) {
            request.put('sites/updateOne', this.form).then(res => {
              if (res.code === '200') {
                this.$message({
                  type: "success",
                  message: "更新信息成功！"
                })
                this.centerDialogVisibleEdit = false
                this.getall()
              } else {
                this.$message({
                  type: "warning",
                  message: res.msg
                })
              }
            })
          }
        })
      }
    },
    handleCurrentChange(pageNum) {
      // 点击分页按钮触发分页
      this.params.pageNum = pageNum
      this.getall()
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.centerDialogVisibleEdit = true
    },
    addinfo() {
      this.centerDialogVisibleAdd = true
    },
    filterTag(value, row) {
      return row.type === value;
    },
    handleLinkClick(row) {
      // window.location.href = `${row.sitepage}`;
      window.open(`${row.sitepage}`, '_blank');
    },
  }
}
</script>

<style>

</style>